﻿
; 安装程序初始定义常量
!define PRODUCT_NAME "VVClass"
!define CN_PRODUCT_NAME "VVClass直播课堂"
!define PRODUCT_VERSION "1.0.0.0"
!define PRODUCT_PUBLISHER "湖南状元堂教育科技有限公司"
!define PRODUCT_WEB_SITE "https://cs8.hyzyt.cn/pc"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor lzma

; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"
!include "x64.nsh"
!include "Winver.nsh"
!include "LogicLib.nsh"
!include "FileFunc.nsh"
!include "packcommon\zyt_common.nsh"
!include "packcommon\zyt_logger.nsh"

; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON ".\favicon.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES

; 安装完成页面
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION InstallExec
!insertmacro MUI_PAGE_FINISH

; 安装卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES
; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChinese"

;出现中文乱码, 放在MUI_LANGUAGE之后
VIAddVersionKey /LANG=2052 ProductName "${PRODUCT_NAME}"
VIAddVersionKey /LANG=2052 ProductVersion "${PRODUCT_VERSION}"
VIAddVersionKey /LANG=2052 Comments "${PRODUCT_NAME}"
VIAddVersionKey /LANG=2052 CompanyName "${PRODUCT_PUBLISHER}"
VIAddVersionKey /LANG=2052 LegalCopyright "${PRODUCT_PUBLISHER} 版权所有 (C) 2020-2022"
VIAddVersionKey /LANG=2052 InternalName "${PRODUCT_NAME}"
;VIAddVersionKey LegalTrademarks "${PRODUCT_PUBLISHER}" ;合法商标
VIAddVersionKey /LANG=2052 OriginalFilename "${PRODUCT_NAME}" ;源文件名
;VIAddVersionKey PrivateBuild "" ;个人内部版本说明
;VIAddVersionKey SpecialBuild "" ;特殊内部本本说明
VIProductVersion "${PRODUCT_VERSION}" ;版本号
VIAddVersionKey /LANG=2052 FileDescription "${PRODUCT_NAME}" ;文件描述(标准信息)
VIAddVersionKey /LANG=2052 FileVersion "${PRODUCT_VERSION}" ;文件版本(标准信息)

; 安装预释放文件
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------

Name "${CN_PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "${PRODUCT_NAME}-${PRODUCT_VERSION}.exe"
InstallDir "$PROGRAMFILES\${PRODUCT_NAME}"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "${PRODUCT_NAME}"
RequestExecutionLevel admin

Var /GLOBAL  g_IsLaunchByUpdate

;开启安装日志
Section "-LogSetOn"
    LogSet on
SectionEnd

Section "MainSection" SEC01
    SetOutPath "$INSTDIR"
    SetOverwrite try

    ;检测旧版本
    Var /GLOBAL UninstPath
    ReadRegStr $UninstPath HKLM64 "${PRODUCT_UNINST_KEY}" "UninstallString"
    ${if} $UninstPath != ""
      ${TLLog}  "检测到旧版本,停止正在运行的 ${PRODUCT_NAME}.exe程序,开始卸载."
      ${TLKillProcess} "${PRODUCT_NAME}"
      # 开始卸载
      Call UninstallProgram
    ${EndIf}

    ${TLLog} "开始安装${PRODUCT_NAME}.exe"
    File ".\x86_64\${PRODUCT_NAME}.exe"
    File /r ".\x86_64\*.*"

    SetAutoClose true
SectionEnd

Section "创建开始菜单" StartMenuSectionIndex
  CreateDirectory "$SMPROGRAMS\${CN_PRODUCT_NAME}"
  ; 创建启动快捷方式
  CreateShortCut "$SMPROGRAMS\${CN_PRODUCT_NAME}\${CN_PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
  ; 创建卸载快捷方式
  CreateShortCut "$SMPROGRAMS\${CN_PRODUCT_NAME}\卸载${CN_PRODUCT_NAME}.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section "创建桌面图标" DesktopIconSectionIndex
  CreateShortCut "$DESKTOP\${CN_PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
SectionEnd


Section "添加卸载信息到控制面板" ControlPanelSectionIndex
  # 生成卸载程序
  SetRegView 64
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "apppath" "$INSTDIR\${PRODUCT_NAME}.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\${PRODUCT_NAME}.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  ;写入安装目录，卸载时用到
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "InstallLocation" "$INSTDIR"
SectionEnd


# 后置操作
Section -Post
SectionEnd



/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/
Section "Uninstall"
  FileOpen $TLFileHandle "$TEMP\uninstall_${PRODUCT_NAME}.log" "a"
  
  #删除开机自启动
  DeleteRegValue HKLM64 "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" "${PRODUCT_NAME}"
  
  Call un.UninstallProgram
  SetAutoClose true
SectionEnd

Function .onInit
    #Allow only one installer instance
    Call CheckSingleInstance

    ${TLLog} "停止正在运行的 ${PRODUCT_NAME}.exe"
    nsExec::Exec 'taskkill /F /IM "${PRODUCT_NAME}.exe"'

	 # 判断安装程序是否由更新程序拉起
	 ${GetParameters} $0
    ${StrContains} $1 "/S" $0
      StrCmp $1 "" notfound
      StrCpy $g_IsLaunchByUpdate 1
    Goto done
    notfound:
      StrCpy $g_IsLaunchByUpdate 0
      RMDir /r "$LOCALAPPDATA\${PRODUCT_NAME}\*"
      RMDir "$LOCALAPPDATA\${PRODUCT_NAME}"
    done:

FunctionEnd

Function .onInstSuccess
    ${If} $g_IsLaunchByUpdate == 1
	  ;普通权限运行程序
      System::Call 'shell32::ShellExecute(i 0, t"open", t"explorer.exe", t" /e,$INSTDIR\${PRODUCT_NAME}.exe", t"$INSTDIR\\", i 0)'
    ${EndIf}

    ${TLLog} "完成${PRODUCT_NAME}程序的安装"
    
    ; 清理下载的安装包
    StrCpy $0 "$LOCALAPPDATA\Temp\${PRODUCT_NAME}*.exe"
    Delete $0
    ${TLLog} "删除下载的安装包,路径:$LOCALAPPDATA.."

FunctionEnd

Function un.onInit
    nsExec::Exec 'taskkill /F /IM "${PRODUCT_NAME}.exe"'
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ，及其所有的组件？" IDYES +2
    Abort
FunctionEnd

Function un.onUninstSuccess
    DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
    MessageBox MB_ICONINFORMATION|MB_OK "${PRODUCT_NAME} 已成功地从你的计算机移除" /SD IDOK
    ${TLFileLog} "成功卸载${PRODUCT_NAME}程序"
    FileClose $TLFileHandle
    Sleep 200

    RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}\*"
    RMDir "$SMPROGRAMS\${PRODUCT_NAME}"
    RMDir /r "$SMPROGRAMS\${CN_PRODUCT_NAME}\*"
    RMDir "$SMPROGRAMS\${CN_PRODUCT_NAME}"
FunctionEnd

Function InstallExec
  ;普通权限运行程序
  System::Call 'shell32::ShellExecute(i 0, t"open", t"explorer.exe", t" /e,$INSTDIR\${PRODUCT_NAME}.exe", t"$INSTDIR\\", i 0)'
FunctionEnd


#仅允许一个安装程序实例
Function CheckSingleInstance
  ${TLLog} "执行到CheckSingleInstance函数,只允许一个安装实例."
  System::Call 'kernel32::CreateMutex(i 0, i 0, t "${PRODUCT_NAME}") i .r1 ?e'
  Pop $R0 
  StrCmp $R0 0 goahead
    MessageBox MB_OK "The installer is already running." 
    ReadRegStr $R0 HKLM64 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "installerhwnd"
    System::Call 'user32::SetForegroundWindow(i R0) i ..'
    Abort
  goahead:
    WriteRegStr HKLM64 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "installerhwnd" $HWNDPARENT
FunctionEnd

